using System;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pampa.WebControls;

namespace Pampa.Common.Pages
{
    /// <summary>
    /// Summary description for CommonPage
    /// </summary>
    public class AbstractPage : System.Web.UI.Page
    {
        public AbstractPage()
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            AddStyles();
            AddLibraries();
        }

        protected virtual void AddStyles()
        {
        }

        protected virtual void AddLibraries()
        {
            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.Common/Url.js");

            RegisterLibrary("Pampa.WebControls/Drag.js");
            RegisterLibrary("Pampa.WebControls/FloatingWindow.js");
            RegisterLibrary("Pampa.WebControls/AlertWindow.js");
        }

        protected void RegisterStyleSheet(string url)
        {
            RegisterStyleSheet(this, url);
        }

        protected void RegisterLibrary(string url)
        {
            RegisterLibrary(this, url);
        }


        #region Static Functions

        public static string GetImagesPath()
        {
            return "/Images";
        }

        public static void RegisterStyleSheet(Page page, string url)
        {
            string id = url.Replace("/", ".");
            Control styleSheet = page.Header.FindControl(id);

            if (styleSheet == null)
            {

                HtmlLink hlink = new HtmlLink();
                hlink.ID = id;
                hlink.Href = GetResourceUrl(url);
                hlink.Attributes["rel"] = "stylesheet";
                hlink.Attributes["text"] = "text/css";

                page.Header.Controls.Add(hlink);
            }
        }

        protected void RegisterPrintStyleSheet(Page page, string url)
        {
            string id = url.Replace("/", ".");
            Control styleSheet = this.Header.FindControl(id);

            if (styleSheet == null)
            {
                HtmlLink hlink = new HtmlLink();
                hlink.ID = id;
                hlink.Href = url;
                hlink.Attributes["rel"] = "stylesheet";
                hlink.Attributes["text"] = "text/css";
                hlink.Attributes["media"] = "print";

                page.Header.Controls.Add(hlink);
            }
        }

        public static void RegisterLibrary(Page page, string url)
        {
            string key = url.Replace("/", ".");
            page.ClientScript.RegisterClientScriptInclude(key, GetResourceUrl(url));
        }

        public static string GetResourceUrl(string url)
        {
            if (url == null)
                return null;

            var properties = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("pampa");
            string resources = properties["resources.path"];

            string appPath = HttpContext.Current.Request.ApplicationPath;

            if ((url.Contains("Pampa")) && (!String.IsNullOrEmpty(resources)))
            {
                appPath = appPath + (appPath == "/" ? "" : "/");
                string type = "";

                if (url.Contains(".css"))
                {
                    type = "Css/";
                }
                else if (url.Contains(".js"))
                {
                    type = "Js/";
                }
                else if (url.Contains(".gif"))
                {
                    type = "Images/";
                }

                string[] urls = url.Split('/');

                if (urls.Length == 2)
                {
                    url = appPath + resources + "/" + urls[0] + "/" + type + urls[1];
                }
                else
                {
                    url = appPath + resources + "/" + url;
                }
            }
            else
            {
                appPath = (appPath == "/") ? "" : appPath;
                url = url.Replace("~", appPath);
            }

            return url;
        }

        #endregion
    }
}